package business_opportunity

import (
	"context"
	"git.mycaigou.com/gfyx/common/rpc_client"
	"git.mycaigou.com/gfyx/micro-gfyx-api/infrastructure/common/constants"
	pb "git.mycaigou.com/gfyx/micro-gfyx-api/proto/micro_gfyx_user_service_proto/micro-gfyx-user-service"
)

type CustomerGroupRepository struct {
}

func NewCustomerGroupRepository() CustomerGroupRepositoryIface {
	return &CustomerGroupRepository{}
}

func (r *CustomerGroupRepository) GetCustomerGroupList(ctx context.Context, request *pb.GetCustomerGroupListRequest) (*pb.GetCustomerGroupListResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewCustomerGroupServiceClient(conn)
	return client.GetCustomerGroupList(ctx, request)
}

func (r *CustomerGroupRepository) GetCustomerGroupDetail(ctx context.Context, request *pb.GetCustomerGroupDetailRequest) (*pb.GetCustomerGroupDetailResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewCustomerGroupServiceClient(conn)
	return client.GetCustomerGroupDetail(ctx, request)
}

func (r *CustomerGroupRepository) SaveCustomerGroup(ctx context.Context, request *pb.SaveCustomerGroupRequest) (*pb.SaveCustomerGroupResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewCustomerGroupServiceClient(conn)
	return client.SaveCustomerGroup(ctx, request)
}

func (r *CustomerGroupRepository) DeleteCustomerGroup(ctx context.Context, request *pb.DeleteCustomerGroupRequest) (*pb.DeleteCustomerGroupResponse, error) {
	conn, ctx, err := rpc_client.GetClientConn(ctx, constants.MICRO_GFYX_USER_SERVICE)
	if err != nil {
		return nil, err
	}
	defer func() {
		_ = conn.Close()
	}()
	client := pb.NewCustomerGroupServiceClient(conn)
	return client.DeleteCustomerGroup(ctx, request)
}
